package com.carp.springframework.beans.factory.support;

import com.carp.springframework.beans.factory.BeanFactory;
import com.carp.springframework.beans.factory.config.BeanDefinition;

import java.util.Objects;

/**
 * @author lile
 * @date 2023/2/2
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) {
        return doGetBean(name, args);
    }

    protected Object doGetBean(String name, Object[] args) {
        Object bean = getSingleton(name);
        if (Objects.nonNull(bean)) {
            return bean;
        }
        return createBean(name, getBeanDefinition(name), args);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args);

}
